package BanyuanClub.Work.Story.Three;

import java.util.Arrays;

public class AliPay {
    private Account[] accounts = new Account[5];

    {
        for (int i = 1; i <= accounts.length ; i++) {
            accounts[i-1] = new Account("1000"+i,"1111",i*1000);
        }
    }

    @Override
    public String toString() {
        return "AliPay{" +
                "accounts=" + Arrays.toString(accounts) +
                '}';
    }

    public Account[] printArr(){
        return accounts;
    }

    public void setAccounts(Account[] accounts) {
        this.accounts = accounts;
    }

    public String transfer(String id, String password, int money, String toId){
        //1校验卡号
        Account I = forId(id);
        if(I == null){
            return "账号不存在";}
        //2校验密码
        if ( !password.equals( I.getPassword())){
            return "密码不正确";}
        //3校验余额
        if (money > I.getMoney()){
            return "余额不足";}
        //4校验对方卡号
        Account You = forId(toId);
        if (You == null){
            return "对方不是人";}
        //5不能转给自己
        if (I.equals(You)){
            return "不能自攻自受";}
        //6开始转账
        I.setMoney(I.getMoney()-money);
        You.setMoney(You.getMoney()+money);
        return "转账成功";
    }
    private Account forId(String id){
        for (Account account:
             accounts) {
            if (id.equals(account.getId())){
                return account;
            }
        }
        return null;
    }
}
